home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / web / noweb / src / icon / noindex.icn < prev    next >
Text File  |  1995-02-24  |  2KB  |  88 lines

  1. #line 6 "noindex.nw"
  2. procedure main(args)
  3.   if *args ~= 1 then 
  4.     write(&errout, "Usage: nomkidx basename")
  5.   else {
  6.     in := get(args)
  7.     if in[-4:0] == ".tex" then in[-4:0] := ".aux"
  8.     else if not (in[-4:0] == ".aux") then in ||:= ".aux"
  9.     gobble(in, idx := [])
  10.     f := open(in[1:-4] || ".nwi", "w") | stop("Cannot write ", in[1:-4] || ".nwi")
  11.     last := ""
  12.     every out := stripcount(!alphasort(idx)) do
  13.       write(f, last ~== out) & last := out
  14.     close(f)
  15.   }
  16.   return
  17. end
  18. #line 23 "noindex.nw"
  19. procedure gobble(name, idx)
  20.   static count
  21.   initial count := 0
  22.   in := open(name) | 
  23. #line 40 "noindex.nw"
  24. { write(&errout, "No file ", name, "."); return }
  25. #line 27 "noindex.nw"
  26.   while line := read(in) do
  27.     line ? 
  28.       if ="\\@input{" & name := tab(upto('}')) & move(1) & pos(0) then
  29.         gobble(name, idx)
  30.       else if ="\\nwixadds{" then 
  31.         put(idx, right(count +:= 1, 6, "0") || " " || tab(0))
  32.   return
  33. end
  34. #line 36 "noindex.nw"
  35. procedure stripcount(s)
  36.   s ? { tab(many(&digits)); tab(many(' ')); return "\\nwixaddsx{" || tab(0) }
  37. end
  38. #line 42 "noindex.nw"
  39. procedure alphasort(x)
  40.   t := table()
  41.   every s := !x do t[s] := sortkey(s)
  42.   t := sort(t, 2)
  43.   l := []
  44.   every put(l, (!t)[1])
  45.   return l
  46. end
  47. #line 51 "noindex.nw"
  48. procedure sortkey(s)
  49.   local count
  50.   map(s) ? {
  51.     count := tab(many(&digits))
  52.     tab(many(' '))
  53.     if ="c}" then return firstkey() || "\n" || count
  54.     else if ="i}" then return firstkey() || "\n" || count
  55.     else stop("not chunk or index")
  56.   }
  57. end
  58. #line 62 "noindex.nw"
  59. procedure firstkey()
  60.   ="{{" | stop("bad index or chunk format")
  61.   s := (remove_control_sequences() ? tab(bal('}', '{', '}') | 0))
  62.   return alphabet(s) || "\n" || s
  63. end
  64. #line 68 "noindex.nw"
  65. procedure alphabet(s)
  66.   static keep
  67.   initial keep := &lcase ++ ' \t'
  68.   r := ""
  69.   s ? {
  70.     while tab(upto(keep)) do 
  71.       if tab(many(' \t')) then
  72.          r ||:= " "
  73.       else
  74.          r ||:= tab(many(&lcase)) | stop("This can't happen")
  75.   }
  76.   r ? { tab(many(' ')) ; return tab(0) }        # strip leading blanks
  77. end
  78. #line 82 "noindex.nw"
  79. procedure remove_control_sequences()
  80.   s := ""
  81.   while s ||:= tab(upto('\\')) do {
  82.     ="\\"
  83.     tab(many(&letters)) | move(1)       # skip
  84.     tab(many(' \t'))
  85.   }
  86.   return s || tab(0)
  87. end
  88.